<?php

/**
 * This is the model class for table "{{issue}}".
 *
 * The followings are the available columns in table '{{issue}}':
 * @property integer $ID
 * @property integer $userID
 * @property integer $attempt_time
 * @property integer $gradeID
 * @property integer $subjectID
 * @property integer $emergency
 * @property integer $quantity_object
 * @property integer $estimate_idle_time
 * @property integer $level_complex
 * @property string $content
 * @property integer $status
 * @property integer $checker
 * @property integer $resolver
 * @property integer $createddate
 * @property integer $modifieddate
 * @property integer $deleted
 * @property integer $notify_date
 */
class Issue extends CActiveRecord
{
		public $image;
	  public $captcha;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Issue the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{issue}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userID, attempt_time, gradeID, subjectID, emergency, quantity_object, content', 'required'),
			array('image','file','allowEmpty'=>false,'types'=>'jpg,jpeg,png,gif',),
			array('userID, attempt_time, gradeID, subjectID, emergency, quantity_object, estimate_idle_time, level_complex, status, checker, resolver, createddate, modifieddate, deleted, notify_date', 'numerical', 'integerOnly'=>true),
			array('captcha','required'),
		  array('captcha','captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, userID, attempt_time, gradeID, subjectID, emergency, quantity_object, estimate_idle_time, level_complex, content, status, checker, resolver, createddate, modifieddate, deleted, notify_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
			return array(
			'ID' => 'ID',
			'userID' => 'User',
			'attempt_time' => 'Attempt Time',
			'gradeID' => 'Lớp',
			'subjectID' => 'Môn học',
			'emergency' => 'Emergency',
			'estimate_idle_time' => 'Estimate Idle Time',
			'level_complex' => 'Level Complex',
			'content' => 'Nội dung',
			'status' => 'Status',
			'resolver' => 'resolver',
			'createddate' => 'Createddate',
			'modifieddate' => 'Modifieddate',
			'deleted' => 'Deleted',
			'notify_date' => 'Notify Date',		
			'quantity_object' => 'Số lượng',
			'image'=>'File',			
			'captcha'=>'Mã xác nhận'
		);
	}

	
protected function beforeSave() {
		if($this->isNewRecord) {
			$this->attempt_time = 0;			
			$this->estimate_idle_time = 123;
			$this->level_complex = 1;					
			$this->createddate =time();	
		}
			$this->modifieddate = time();
		return parent::beforeSave();
	}
	
	//function getGrade
	public function getGrade() {
		$array = array();
		$grade = Grade::model()->findAllByAttributes(array('deleted'=>0));
		if(!empty($grade)) {
			foreach ($grade as $g) {
				$array[$g->ID] = $g->grade; 
			}
		}
		return $array;
	}
	
//function getGrade
	public function getSubject() {
		$array = array();
		$subject = Subject::model()->findAllByAttributes(array('deleted'=>0));
		if(!empty($subject)) {
			foreach ($subject as $s) {
				$array[$s->ID] = $s->subject; 
			}
		}
		return $array;
	}
	
		
	
	//function Get Available Resolver
	public function getResolver()
	{
		$resolver = Yii::app()->db->createCommand()
            ->select('u.*')
            ->from('tbl_user as u')
            ->join('tbl_user_role as ur', 'ur.userID = u.ID')
            ->join('tbl_user_status as us','us.userID=u.ID')
            ->where('ur.roleID=3 AND us.statusID =1')
            ->queryRow(); 
            
    if (!empty($resolver))
    {
      return $resolver['ID'];	// cho nay lay ho ten dc khong e         
    }
    else
    {
    	return 0;
    }
	}
	
	//function Get Available Resolver
	public function getChecker()
	{
		$resolver = Yii::app()->db->createCommand()
            ->select('u.*')
            ->from('tbl_user as u')
            ->join('tbl_user_role as ur', 'ur.userID = u.ID')
            ->join('tbl_user_status as us','us.userID=u.ID')
            ->where('ur.roleID=3 AND us.statusID =1')
            ->queryRow(); 
            
    if (!empty($resolver))
    {
      return $resolver['ID'];	// cho nay lay ho ten dc khong e         
    }
    else
    {
    	return 0;
    }
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID);
		$criteria->compare('userID',$this->userID);
		$criteria->compare('attempt_time',$this->attempt_time);
		$criteria->compare('gradeID',$this->gradeID);
		$criteria->compare('subjectID',$this->subjectID);
		$criteria->compare('emergency',$this->emergency);
		$criteria->compare('quantity_object',$this->quantity_object);
		$criteria->compare('estimate_idle_time',$this->estimate_idle_time);
		$criteria->compare('level_complex',$this->level_complex);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('checker',$this->checker);
		$criteria->compare('resolver',$this->resolver);
		$criteria->compare('createddate',$this->createddate);
		$criteria->compare('modifieddate',$this->modifieddate);
		$criteria->compare('deleted',$this->deleted);
		$criteria->compare('notify_date',$this->notify_date);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}